package com.book.web.controller;

import com.book.domain.Book;
import com.book.service.BookService;
import com.book.service.impl.BookServiceImpl;
import com.book.utils.BeanUtil;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/book")
public class BookServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String operation = req.getParameter("operation");
        if("list".equals(operation)){
            this.list(req,resp);
        }else if("toAdd".equals(operation)){
            this.toAdd(req,resp);
        }else if("save".equals(operation)){
            this.save(req, resp);
        }else if("toEdit".equals(operation)){
            this.toEdit(req,resp);
        }else if("edit".equals(operation)){
            this.edit(req,resp);
        }else if("delete".equals(operation)){
            this.delete(req,resp);
        }
        
        
    }

    private void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Book book = BeanUtil.fillBean(req,Book.class);
        BookService bookService = new BookServiceImpl();
        bookService.delete(book);
        resp.sendRedirect(req.getContextPath()+"/book?operation=list");
    }

    private void edit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //将数据获取到，封装成一个对象
        Book book = BeanUtil.fillBean(req,Book.class);
        //调用业务层接口save
        BookService bookService = new BookServiceImpl();
        bookService.update(book);
        //跳转回到页面list
        resp.sendRedirect(req.getContextPath()+"/book?operation=list");
    }

    private void toEdit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //查询要修改的数据findById
        String id = req.getParameter("id");
        BookService bookService = new BookServiceImpl();
        Book book = bookService.findById(id);
        //将数据加载到指定区域，供页面获取
        req.setAttribute("book",book);
        //跳转页面
        req.getRequestDispatcher("/WEB-INF/pages/store/book/update.jsp").forward(req,resp);
    }

    private void save(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //将数据获取到，封装成一个对象
        Book book = BeanUtil.fillBean(req,Book.class);
        //调用业务层接口save
        BookService bookService = new BookServiceImpl();
        bookService.save(book);
        //跳转回到页面list
        //list(request,response);
        resp.sendRedirect(req.getContextPath()+"/book?operation=list");
    }

    private void toAdd(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getRequestDispatcher("/WEB-INF/pages/store/book/add.jsp").forward(req,resp);
    }

    private void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        BookService bookService = new BookServiceImpl();
        int page = 1;
        int size = 5;
        if(StringUtils.isNotBlank(req.getParameter("page"))){
            page = Integer.parseInt(req.getParameter("page"));
        }
        if(StringUtils.isNotBlank(req.getParameter("size"))){
            size = Integer.parseInt(req.getParameter("size"));
        }
        PageInfo all = bookService.findAll(page, size);
        //将数据保存到指定的位置
        req.setAttribute("page",all);
        //跳转页面
        req.getRequestDispatcher("/WEB-INF/pages/store/book/list.jsp").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
